package br.com.advance.automacao.web.utils;

import java.io.IOException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class SegurancaMBean {

	public void verificaLogado() throws IOException {
		verificaLogado(FacesContext.getCurrentInstance().getExternalContext());
	}

	public static void verificaLogado(ExternalContext externalContext) throws IOException {
		if(!isLogado(externalContext)) {
			externalContext.redirect("/advance/index.jsf");
		}
	}

	public boolean isLogado() throws IOException {
		return isLogado(FacesContext.getCurrentInstance().getExternalContext());
	}

	public static boolean isLogado(ExternalContext externalContext) throws IOException {
		Object logado = externalContext.getSessionMap().get("logado");
		if(logado == null || !Boolean.valueOf(logado.toString())) {
			return false;
		}
		return true;
	}

	public boolean temPermissao() {
		return false;
	}
}
